package cc.uncarbon.module.appapi.web.app;

import cc.uncarbon.framework.core.page.PageParam;
import cc.uncarbon.framework.core.page.PageResult;
import cc.uncarbon.framework.web.model.response.ApiResult;
import cc.uncarbon.module.appapi.constant.AppApiConstant;
import cc.uncarbon.module.appapi.model.request.OrdersDTO;

import cc.uncarbon.module.appapi.model.request.OrderListDTO;
import cc.uncarbon.module.appapi.model.vo.OrdersDetailVO;
import cc.uncarbon.module.appapi.model.vo.OrdersVO;
import cc.uncarbon.module.appapi.service.OrdersService;
import cc.uncarbon.module.appapi.service.WechatPayService;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "小程序用户端订单管理")
@RequestMapping(AppApiConstant.HTTP_API_URL_PREFIX + "/api/v1")
@RequiredArgsConstructor
@RestController
@Slf4j
public class OrdersController {

    private final OrdersService ordersService;
    private final WechatPayService wechatPayService;
    @Operation(summary = "统一下单")
    @PostMapping("/orders/create")
    public ApiResult<Long> createOrder(@RequestBody @Validated OrdersDTO dto) {
       Long id= ordersService.createOrder(dto);
        return ApiResult.data(id);
    }
    @Operation(summary = "我的订单")
    @GetMapping("/orders/list")
    public ApiResult<PageResult<OrdersVO>> getList(PageParam pageParam, OrderListDTO dto){

        return ApiResult.data(ordersService.getordersList(pageParam,dto));
    }
    @Operation(summary = "订单详情")
    @GetMapping("/orders/detail/{id}")
    public ApiResult<OrdersDetailVO> getOrderDetail(@PathVariable("id") Long id){
        OrdersDetailVO detailVO=ordersService.getOrderDetail(id);
        return ApiResult.data(detailVO);
    }

    @Operation(summary = "取消订单")
    @GetMapping("/orders/cancel/{id}")
    public ApiResult cancelOrder(@PathVariable("id") Long id){
        ordersService.cancelOrder(id);

        return ApiResult.success("取消成功");
    }
    @GetMapping("/pay/{id}")
    public ApiResult<PrepayWithRequestPaymentResponse> unifiedOrder(@PathVariable("id") Long id) {
        PrepayWithRequestPaymentResponse payVo =   ordersService.unifiedOrderV3(id);
        return ApiResult.data(payVo);
    }
    @PostMapping("/pay/notify")
    public Map<String, Object> payNotify(HttpServletRequest request) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            ordersService.payNotify(request);
            result.put("code", "SUCCESS");
            result.put("message", "成功");
            return result;
        } catch (Exception e) {
            result.put("code", "FAIL");
            result.put("message", "失败");
        }
        return result;

    }
    @PostMapping("/refund/notify")
    public Map<String, Object> refundNotify(HttpServletRequest request) {

        HashMap<String, Object> result = new HashMap<>();
        try {
            ordersService.refundNotify(request);
            result.put("code", "SUCCESS");
            result.put("message", "成功");
            return result;
        } catch (Exception e) {
            result.put("code", "FAIL");
            result.put("message", "失败");
        }
        return result;

    }


}
